package cn.christ.com.mobilesafe.common;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.umeng.analytics.MobclickAgent;

/**
 * Created by Administrator on 2018/7/14.
 */

public abstract class BaseFragment extends Fragment implements IUIOperation {
    //fragment显示的布局
    public View root;
    //管理fragment的activity
    public BaseActivity activity;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //初始化activity对象
        activity = (BaseActivity) getActivity();
    }

    @Override
    public void onResume() {
        super.onResume();
        //友盟
        MobclickAgent.onPageStart(activity.getLocalClassName());
    }

    @Override
    public void onPause() {
        super.onPause();
        //友盟
        MobclickAgent.onPageEnd(activity.getLocalClassName());
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (root == null) {
            root = Global.inflate(getLayoutRes());
            initView();
            initListener();
            initData();
        } else {
            //解除root与其父控件的关系
            ViewGroup parent = (ViewGroup) root.getParent();
            if (parent != null) {
                parent.removeView(root);
            }
        }
        return root;
    }


    /**
     * 不需要强转
     *
     * @param id
     * @param <T>
     * @return
     */
    public <T> T findView(int id) {
        return (T) root.findViewById(id);
    }

}
